<?php

// +----------------------------------------------------------------------
// | Static Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-static
// | github 代码仓库：https://github.com/zoujingli/think-plugs-static
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\common\model\index\BannerModel;
use app\common\model\index\CategoryModel;
use app\common\model\index\LoginModel;
use app\common\model\index\LoginTokenModel;
use phpseclib3\Net\SSH2;
use think\admin\extend\DataExtend;

/**
 * 首页
 * @package app\admin\controller
 * @author wangqinghui
 * @DateTime 2024-09-03 11:29:00
 */
class Index extends BaseIndex
{
    protected $check_login = true;
    /**
     * 首页列表
     * @author wangqinghui
     * @DateTime 2024-09-12 11:40:58
     * @return void
     */
    public function index()
    {
        $this->title = lang('ERP实训');
        // 获取学科数据
        $map = [
            ['status', '=', 1],// 启用
        ];
        $category_list = CategoryModel::mk()->where($map)->order('sort desc,id asc')->select()->toArray();
        $category_data = DataExtend::arr2tree($category_list);
        // 获取轮播图
        $map = [
            ['status', '=', 1],// 启用
            ['type',   '=', 0]// 轮播

        ];
        $banner_list = BannerModel::mk()->where($map)->order('sort desc,id asc')->select()->toArray();
        // 获取首页介绍
        $map = [
            ['status', '=', 1],// 启用
            ['type',   '=', 1] // 首页介绍
        ];
        $intro_data = BannerModel::mk()->where($map)->order('sort desc,id asc')->select()->toArray();

        // 实践课程导航
        $course_nav_id = 2;
        $this->fetch('index', ['category' => $category_data, 'banners' => $banner_list, 'intros' => $intro_data, 'course_nav_id' => $course_nav_id]);
    }


    /**
     * 登陆
     *
     * @return void
     */
    public function login ()
    {
        $host     = $this->request->post('host');
        $username = $this->request->post('username');
        $password = $this->request->post('password');

        $ssh = new SSH2($host);
        if (!$ssh->login($username, $password)) {
          $this->error('登陆失败');
        }

        // 将登录数据存储下来
        $login_obj = LoginModel::mk()->where('host', $host)->where('username', $username)->find();

        if (!$login_obj) {
            $login_obj = LoginModel::createData($host, $username, $password);
        }

        // 生成token 并存储
        $token = md5(time().mt_rand(100, 999));

        $login_token_obj = new LoginTokenModel();

        $login_token_obj->user_id = $login_obj->id;
        $login_token_obj->token   = $token;
        $login_token_obj->expire_time = time() + 10 * 3600;
        $login_token_obj->save();

        $this->success('登陆成功', ['token'=>$token]);
    }


    public function test ()
    {
        $aa = file_get_contents('https://sc04.alicdn.com/kf/H55defe8842d04f3c83076379c7f9abc9p.jpg_100x100.jpg');
        dump($aa);
    }
}
